package com.marvels.approve.manager.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.approve.consum.ApproveConsumerFinancialApi;
import com.marvels.approve.consum.ApproveConsumerLoanApplyApi;
import com.marvels.approve.consum.ApproveConsumerProductApi;
import com.marvels.approve.loan.dto.CascadeChannelDto;
import com.marvels.approve.loan.dto.McptChannelDto;
import com.marvels.approve.loan.dto.McptProductType;
import com.marvels.approve.loan.dto.UserChannelInfoParamDTO;
import com.marvels.approve.loan.dto.UserChannelInfoResultDTO;
import com.marvels.approve.manager.dao.ApprovalProcessDao;
import com.marvels.approve.manager.dto.ApprovalProcessConditionDTO;
import com.marvels.approve.manager.dto.ApprovalProcessInfoDTO;
import com.marvels.approve.manager.dto.ApproveManageScreeneDTO;
import com.marvels.approve.manager.dto.CollectAccountDTO;
import com.marvels.approve.manager.dto.LoanStateEnum;
import com.marvels.approve.manager.service.ApproveManagerService;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.ServerProperties;
import com.marvels.system.permission.dto.SysUserDTO;
import com.marvels.system.permission.service.SysUserService;

import cn.hutool.core.util.StrUtil;

@Service
public class ApproveManagerImpl implements ApproveManagerService {

	@Autowired
	private ApproveConsumerFinancialApi consumerFinancialApi;
	
	@Autowired
	private ApproveConsumerLoanApplyApi consumerLoanApplyApi;
	
	@Autowired
	private SysUserService sysUserService;
	
	@Value("${config.properties.path}")
	private String configPropertiesPath;
	
	@Override
	public ApproveManageScreeneDTO screeneCondition() throws MarvelsBusinessException {
		List<String> ignoreUserCodeList = new ArrayList<String>();
		String ignoreUserCodes= ServerProperties.getValue(configPropertiesPath, "common.properties", "ignore_user_code");
		if(StrUtil.isNotEmpty(ignoreUserCodes)){
			ignoreUserCodeList = Arrays.asList(ignoreUserCodes.split(","));
		}
		// 处理人列表
		List<SysUserDTO> dealUserList = sysUserService.selectUserListIgnore(ignoreUserCodeList);
		UserChannelInfoParamDTO paramDto = new UserChannelInfoParamDTO();
		//渠道列表
		List<UserChannelInfoResultDTO> channelList = consumerLoanApplyApi.selectUserChannelList(paramDto);
		//状态列表
		List<Map<String, String>> stateList = LoanStateEnum.getStateList("1");
		ApproveManageScreeneDTO dto = new ApproveManageScreeneDTO();
		dto.setStatesList(stateList);
		dto.setChannelList(channelList);
		dto.setDealUserList(dealUserList);
		return dto;
	}

	@Override
	public PageInfo<ApprovalProcessInfoDTO> listQuery(ApprovalProcessConditionDTO dto) throws MarvelsBusinessException {
		PageInfo<ApprovalProcessInfoDTO> pageInfo = new PageInfo<ApprovalProcessInfoDTO>();
		if(StringUtils.isNoneEmpty(dto.getStates())){
			if(!"yuqi".equals(dto.getStates())){
				dto.setStateList(Arrays.asList(dto.getStates().split(",")));
			}
		}
		PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
		pageInfo = consumerLoanApplyApi.loanListQuery(dto);
		//特殊处理
		List<String> queryloanIds = new ArrayList<String>();
		for (ApprovalProcessInfoDTO approvalProcessInfoDTO : pageInfo.getList()) {
			if("yuqi".equals(dto.getStates())){
				approvalProcessInfoDTO.setCaseStatusName(LoanStateEnum.OVERDUE.getStateName());
			}else{
				//还款去查询状态
				if("D01".equals(approvalProcessInfoDTO.getCaseStatus())){
					queryloanIds.add(approvalProcessInfoDTO.getLoanId());
				}
				approvalProcessInfoDTO.setCaseStatusName(LoanStateEnum.getNameByState(approvalProcessInfoDTO.getCaseStatus()));
			}
		}
		//为了减少远层调用，直接查回一个map
		Map<String, String> overdueMap = consumerFinancialApi.selectAccountOverdueInfo(queryloanIds);
		if(overdueMap != null){
			for (ApprovalProcessInfoDTO approvalProcessInfoDTO : pageInfo.getList()) {
				//存在记录则说明是逾期
				String status = overdueMap.get(approvalProcessInfoDTO.getLoanId());
				if(StringUtils.isNotEmpty(status)){
					approvalProcessInfoDTO.setCaseStatusName(LoanStateEnum.OVERDUE.getStateName());
				}
			}
		}
		return pageInfo;
	}

}
